package com.cc.visitor.controller;

import com.cc.alltype.ResponseResult;
import com.cc.alltype.wechat.QrType;
import com.cc.visitor.annotation.VerifySignature;
import com.cc.visitor.entity.Employee;
import com.cc.visitor.entity.wechat.CreateQrTicketReqParam;
import com.cc.visitor.entity.wechat.templateMessage.TemplateMsgParam;
import com.cc.visitor.utils.RequestValidator;
import com.cc.visitor.utils.RestTemplateUtils;
import com.cc.visitor.utils.TemplateMsgUtils;
import com.cc.visitor.utils.WechatUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author chanchaw
 * @create 2024-09-19 8:57
 */
@CrossOrigin
@RestController
@RequestMapping("/test")
public class TestController {
    @Autowired
    private TemplateMsgUtils templateMsgUtils;
    private ObjectMapper mapper = null;
    @Autowired
    private WechatUtils wechatUtils;
    @Resource
    private RestTemplate restTemplate;
    @Autowired
    private RestTemplateUtils restTemplateUtils;

    @PostConstruct
    public void init(){
        mapper = new ObjectMapper();
    }
    @VerifySignature
    @GetMapping("/now")
    public ResponseResult getTime(){
        System.out.println("请求了：/test/now");
        return new ResponseResult(LocalDateTime.now());
    }
    @PostMapping("/exception")
    public ResponseResult testException(){
        //throw new BusinessException("testException");
        int i = 1/0;
        return new ResponseResult(LocalDateTime.now());
    }
    @PostMapping("/restTemplateGet")
    public Object testRestTemplateGet(){
        List<Object> userMills = restTemplate.getForObject("https://www.xdfznh.club/wxpadbe/userMillController/selectMills/ovC8bs78-EymCeGDgtycJKOXGTh0", List.class);
        System.out.println(userMills);
        return userMills;
    }

    @PostMapping("/restTemplatePost")
    public Object testRestTemplatePost() throws JsonProcessingException {
        HashMap<String, Object> map = new HashMap<>();
        map.put("openid","ovC8bs78-EymCeGDgtycJKOXGTh0");
        map.put("mill", "新达印染");
        Object o = restTemplateUtils.postJson("https://www.xdfznh.club/wxpadbe/user/isMillInnerGeneral", map, Object.class);
        System.out.println(o);
        return o;
    }

    @PostMapping("/sceneQRTicket")
    public ResponseResult getSceneQRTicketTemp(@RequestBody CreateQrTicketReqParam param) throws JsonProcessingException {
        Integer permanent = param.getPermanent();
        QrType qrTypeEnum = QrType.ofId(permanent);
        return ResponseResult.ok("成功", wechatUtils.getQRTicket(qrTypeEnum,param.getSceneFull()));
    }

    @PostMapping("/getObjectJson")
    public ResponseResult getObjectJson(@RequestBody Object o) throws JsonProcessingException {
        ObjectWriter ow = mapper.writer().withDefaultPrettyPrinter();
        String json = ow.writeValueAsString(o);
        ArrayList<String> strList = new ArrayList<>();
        strList.add(json);
        String s = json.replaceAll("\r\n ", "");// 移除所有回车、换行符
        String s1 = s.replaceAll("\r\n", "");
        strList.add(s1);
        return ResponseResult.ok(strList);
    }

    @PostMapping("/testJsonString")
    public ResponseResult testJsonString() throws JsonProcessingException {
        Employee employee = new Employee();
        employee.setIid(11);
        employee.setSid("1001");
        employee.setSname("chanchaw");

        ObjectWriter ow = mapper.writer().withDefaultPrettyPrinter();
        String paramJsonString = ow.writeValueAsString(employee);
        System.out.println(paramJsonString);

        Employee employee1 = mapper.readValue(paramJsonString, Employee.class);
        System.out.println("转换后的对象的名称：" + employee1.getSname());
        return ResponseResult.ok();
    }

    @PostMapping("/sendTemplateMsg")
    public ResponseResult sendTemplateMsg(@RequestBody TemplateMsgParam param) throws Exception {
        //templateMsgUtils.pushNavigate2register(param);
        templateMsgUtils.pushMap();
        return ResponseResult.ok("推送模板消息成功！");
    }

    @PostMapping("/getSignature")
    public ResponseResult getSignature(@RequestBody Map<String, String> params) throws Exception {
        return ResponseResult.ok(RequestValidator.generateSignature(params,"showa"));
    }
    @PostMapping("/getMpH5Url")
    public ResponseResult getMpH5Url(@RequestBody String url) throws Exception {
        return ResponseResult.ok(WechatUtils.getMpH5Url(url));
    }
    @PostMapping("/pushInterviewee2Audit/{visitId}")
    public ResponseResult pushInterviewee2Audit(@PathVariable Integer visitId) throws Exception {
        templateMsgUtils.pushInterviewee2Audit(visitId);
        return ResponseResult.ok("调用API推送给受访者提醒审核的模板消息");
    }
    @PostMapping("/pushVisitorAudited/{visitId}")
    public ResponseResult pushVisitorAudited(@PathVariable Integer visitId) throws Exception {
        templateMsgUtils.pushVisitorAudited(visitId);
        return ResponseResult.ok("调用API推送给访客，提醒访单已经被审核通过");
    }
    @PostMapping("/pushIntervieweeEntry/{visitId}")
    public ResponseResult pushIntervieweeEntry(@PathVariable Integer visitId) throws Exception {
        templateMsgUtils.pushIntervieweeEntry(visitId);
        return ResponseResult.ok("调用API推送给受访者，访客已进场");
    }
    @PostMapping("/pushIntervieweeLeave/{visitId}")
    public ResponseResult pushIntervieweeLeave(@PathVariable Integer visitId) throws Exception {
        templateMsgUtils.pushIntervieweeLeave(visitId);
        return ResponseResult.ok("调用API推送给受访者，访客已离场");
    }
    @PostMapping("/pushRegister")
    public ResponseResult pushRegister(@RequestBody String openId) throws Exception {
        templateMsgUtils.pushRegister(openId,10);
        return ResponseResult.ok("调用API推送给访客，提醒注册");
    }
    @PostMapping("/pushIntervieweeAuditedUnUse")
    public ResponseResult pushIntervieweeAuditedUnUse() throws Exception {
        return ResponseResult.ok(templateMsgUtils.pushIntervieweeAuditedUnUse());
    }

}
